﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
using Utility.Network;

namespace Utility.Network
{
    class Client
    {
        public MessagePacket SendAndReceive(IPAddress ip, int port, MessagePacket msg)
        {
            Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSock.ReceiveTimeout = 0;
            clientSock.Connect(ip, port);
            MemoryStream tmp = Serializer.Serialize(msg);
            byte[] buf = tmp.ToArray();
            tmp.Close();

            byte[] head = BitConverter.GetBytes(buf.Length);
            clientSock.Send(head);
            byte[] recvBuf = new byte[1024];
            clientSock.Receive(recvBuf);

            clientSock.Send(buf);
            
            clientSock.Receive(recvBuf);
            int totalSize = BitConverter.ToInt32(recvBuf,0);
            int totalRecv = 0;
            MemoryStream mStream = new MemoryStream();
            mStream.Position = 0;
            while (totalRecv<totalSize)
            {
                int count = clientSock.Receive(recvBuf, 1024, 0);
                totalRecv += count;
                if (count == 0)
                {
                    continue;
                }
                else
                {
                    mStream.Write(recvBuf, 0, count); //将接收到的数据写入内存流
                }
            }
            mStream.Flush();
            mStream.Position = 0;

            MessagePacket reply=null;
            if (mStream.Capacity > 0)
            {
                reply = Serializer.DeSerialize(mStream) as MessagePacket;
            }
            mStream.Close();
            clientSock.Close();
            return reply;
        }
        private static void DealWithReplyMessage(MessagePacket msg)
        {
        }
        /*public void SendMessage(string ip, int port,MessagePacket msg)
        {
            try
            {
                clientsocket = new TcpClient(ip, port);
            }
            catch
            {
                //server cannot be connected
            }
            NetworkStream ns = clientsocket.GetStream();
            byte [] buf = Serializer.Serialize(msg).ToArray();
            ns.Write(buf, 0, buf.Length);

            byte[] recvBuf = new byte[1024];
            MemoryStream mStream = new MemoryStream();
            mStream.Position = 0;
            while (true)
            {
                int count = ns.Read(recvBuf, 1024, 0);
                if (count == 0)
                {
                    break;
                }
                else
                {
                    mStream.Write(recvBuf, 0, count); //将接收到的数据写入内存流
                }
            }
            mStream.Flush();
            mStream.Position = 0;

            if (mStream.Capacity > 0)
            {
                MessagePacket reply = Serializer.DeSerialize(mStream, typeof(MessagePacket)) as MessagePacket;
                DealWithReplyMessage(msg);
            }
            else
            {
                Console.WriteLine("接收到服务器的返回数据为空。");
            }

            clientsocket.Close();
            ns.Close();
        }*/
        
        //TcpClient clientsocket;
    }
}
